package cn.tedu.hospitalgroupproject.server.content.controller;

import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.JsonResult;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.DiagnosisReport;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DiagnosisReportAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DiagnosisReportUpdateParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.AppointmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DiagnosisReportListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DiagnosisReportStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDiagnosisReportService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/management/diagnosisReports/")
@Api(tags="1.6 管理部分-诊断报告管理")
public class DiagnosisReportController {

    @Autowired
    private IDiagnosisReportService diagnosisReportService;

    public DiagnosisReportController() {
        log.info("创建诊断报告控制对象");
    }


    @ApiOperation("添加诊断报告")
    @ApiOperationSupport(order = 150)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "报告ID", required = true, dataType = "long")
    })
    @PostMapping("/add-new")
    public JsonResult addNew(@Validated DiagnosisReportAddNewParam diagnosisReportAddNewParam) {
        log.debug("开始处理【新建诊断报告数据】的请求，添加类：{}", diagnosisReportAddNewParam);
        diagnosisReportService.addNew(diagnosisReportAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除诊断报告")
    @ApiOperationSupport(order = 160)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "报告ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "删除诊断报告数据失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【删除指定报告ID下的诊断报告数据】的请求，参数：{}", id);
        diagnosisReportService.delete(id);
        return JsonResult.ok();
    }
    @ApiOperation("修改诊断报告信息")
    @ApiOperationSupport(order = 350)
    @PostMapping("/{id:[0-9]+}/update/info")
    public JsonResult updateInfoById(@Validated DiagnosisReportUpdateParam diagnosisReportUpdateParam) {
        log.debug("开始处理【修改诊断报告信息】的请求，参数：{}", diagnosisReportUpdateParam);
        diagnosisReportService.updateInfoById(diagnosisReportUpdateParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID查询诊断报告信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "报告ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "获取报告信息失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询报告】的请求，参数：{}", id);
        DiagnosisReportStandardVO diagnosisReportStandardVO = diagnosisReportService.getStandardById(id);
        return JsonResult.ok(diagnosisReportStandardVO);
    }


    @ApiOperation("查询诊断报告列表")
    @ApiOperationSupport(order = 220)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public JsonResult listDiagnosisReport(Integer page, String queryType) {
        log.debug("开始处理【查询诊断报告列表】请求，页码：{},查询类型:{}", page,queryType);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DiagnosisReportListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = diagnosisReportService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = diagnosisReportService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("根据创建时间查询诊断报告列表")
    @ApiOperationSupport(order = 221)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @RequestMapping("/{diagnosisTime}/diagnosisTime/list")
    public JsonResult listDiagnosisReportByCreateTime(Integer page, String queryType,@PathVariable("diagnosisTime") String gmtCreate) {
        log.debug("开始处理【根据报告创建时间查询诊断报告列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DiagnosisReportListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = diagnosisReportService.listByCreateTime(1, Integer.MAX_VALUE,gmtCreate);
        } else {
            pageData = diagnosisReportService.listByCreateTime(pageNum,gmtCreate);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("根据患者姓名查询诊断报告列表")
    @ApiOperationSupport(order = 230)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @RequestMapping("/{patientName}/patientName/list")
    public JsonResult listDiagnosisReportByPatientName(Integer page, String queryType,@PathVariable String patientName) {
        log.debug("开始处理【根据患者姓名查询诊断报告列表】请求，页码：{},请求类型:{},患者姓名:{}", page,queryType,patientName);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DiagnosisReportListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = diagnosisReportService.listByPatientName(1, Integer.MAX_VALUE,patientName);
        } else {
            pageData = diagnosisReportService.listByPatientName(pageNum,patientName);
        }
        return JsonResult.ok(pageData);
    }

}
